package com.exp.quick_exec;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.apache.commons.codec.EncoderException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.border.TitledBorder;

/*class MyJDialog extends JDialog {
	public MyJDialog(BuilderGui frame) {
		super(frame, "第一个JDialog窗体", true);// 实例化一个JDialog类对象，指定对话框的父窗体、标题、类型
		Container container = getContentPane();// 创建一个容器
		container.add(new JLabel("这是一个对话框"));
		setBounds(120, 120, 100, 100);
	}
}
//				new MyJDialog(MyFrame.this).setVisible(true);// 使MyJDialog窗体可见

*/
public class BuilderGui extends JFrame {// 创建新类

	public BuilderGui() {
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
			  SwingUtilities.updateComponentTreeUI(this);
			  pack();
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		} 

		final Container container = getContentPane();// 创建一个容器
//		GridLayout layout=new GridLayout(3,3);
		container.setLayout(new GridBagLayout());
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder("生成器"));
		panel.setLayout(new GridBagLayout());
		
		JLabel sourceModuleLabel = new JLabel("源模块");
		sourceModuleLabel.setHorizontalAlignment(JLabel.RIGHT);
		panel.add(sourceModuleLabel,new GBC(0, 0).setFill(GBC.BOTH).setWeight(1, 0));
		
		final JTextField sourceModuleTextField = new JTextField("quick_backend");
		panel.add(sourceModuleTextField,new GBC(0, 1).setFill(GBC.BOTH).setWeight(2, 0).setInsets(2));
		
		JLabel targetModuleLabel = new JLabel("目标模块");
		targetModuleLabel.setHorizontalAlignment(JLabel.RIGHT);
		panel.add(targetModuleLabel,new GBC(1, 0).setFill(GBC.BOTH).setWeight(1, 0));
		
		final JTextField targetModuleTextField = new JTextField("module0");
		panel.add(targetModuleTextField,new GBC(1, 1).setFill(GBC.BOTH).setWeight(2, 0).setInsets(2));
		
		JLabel sourceEntityLabel = new JLabel("源实体");
		sourceEntityLabel.setHorizontalAlignment(JLabel.RIGHT);
		panel.add(sourceEntityLabel,new GBC(2, 0).setFill(GBC.BOTH).setWeight(1, 0));
		
		final JTextField sourceEntityTextField = new JTextField("entityExample");
		panel.add(sourceEntityTextField,new GBC(2, 1).setFill(GBC.BOTH).setWeight(2, 0).setInsets(2));
		
		JLabel targetEntityLabel = new JLabel("目标实体");
		targetEntityLabel.setHorizontalAlignment(JLabel.RIGHT);
		panel.add(targetEntityLabel,new GBC(3, 0).setFill(GBC.BOTH).setWeight(1, 0));
		
		final JTextField targetEntityTextField = new JTextField("entity0");
		panel.add(targetEntityTextField,new GBC(3, 1).setFill(GBC.BOTH).setWeight(2, 0).setInsets(2));
		
		
		
		JButton submitButton = new JButton("确定");
		submitButton.addActionListener(new ActionListener() {// 为按钮添加鼠标单击事件
			public void actionPerformed(ActionEvent e) {
				String sourceModule = sourceModuleTextField.getText();
				String targetModule = targetModuleTextField.getText();
				String sourceEntity = sourceEntityTextField.getText();
				String targetEntity = targetEntityTextField.getText();
				
				try {
					new QuickBuilder().setSourceModule(sourceModule).setTargetModule(targetModule)
					.setSourceEntity(sourceEntity).setTargetEntity(targetEntity)
					.exec();
					JOptionPane.showMessageDialog(container, "代码生成成功！", "消息",  JOptionPane.WARNING_MESSAGE);
				} catch (IOException e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(container, e1, "消息",  JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		panel.add(submitButton,new GBC(-1, 1).setAnchor(GBC.EAST).setInsets(15, 0, 0, 0));
		container.add(panel,new GBC(0,0).setFill(GBC.BOTH).setWeight(1, 1).setInsets(20));
		
		setSize(400, 250);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setVisible(true);
	}
	
	public static void main(String[] args) {
		new BuilderGui();
	}
}

